package cards.service;

import java.util.Date;
import java.util.List;

import cards.dao.CardActivityDao;
import cards.dao.UserDao;
import cards.entity.CardActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class CardActivityService {
    @Autowired
    private CardActivityDao cardActivityDao;


    public String addCardActivity(String title,String publicityPicture,Date startTime,Date endTime,String cardType,
                                  String cardPicture1,String cardPicture2,String cardPicture3,String cardPicture4,String cardPicture5,String collectionRules,
                                  float cardProbability1,float  cardProbability2,float cardProbability3,float cardProbability4, float cardProbability5,
                                  Date createTime,int status,int participants,int fullCollectors) {
        CardActivity cardActivity = new CardActivity();
        if (cardActivityDao.findCardActivityByExactTitle(title) == null) {
            cardActivity.setTitle(title);
            cardActivity.setPublicityPicture(publicityPicture);
            cardActivity.setStartTime(startTime);
            cardActivity.setEndTime(endTime);
            cardActivity.setCardType(cardType);
            cardActivity.setCardPicture1(cardPicture1);
            cardActivity.setCardPicture2(cardPicture2);
            cardActivity.setCardPicture3(cardPicture3);
            cardActivity.setCardPicture4(cardPicture4);
            cardActivity.setCardPicture5(cardPicture5);
            cardActivity.setCollectionRules(collectionRules);
            cardActivity.setCardProbability1(cardProbability1);
            cardActivity.setCardProbability2(cardProbability2);
            cardActivity.setCardProbability3(cardProbability3);
            cardActivity.setCardProbability4(cardProbability4);
            cardActivity.setCardProbability5(cardProbability5);
            cardActivity.setCreateTime(new Date());
            cardActivity.setStatus(status);
            cardActivity.setParticipants(participants);
            cardActivity.setFullCollectors(fullCollectors);
            cardActivityDao.addCardActivity(cardActivity);
            return "redirect:/admin/unattended";
        } else {
            return "addCardActivity";
        }
    }

    public String updateCardActivity(String string_id,String title,String publicityPicture,Date startTime,Date endTime,String cardType,
                                     String cardPicture1,String cardPicture2,String cardPicture3,String cardPicture4,String cardPicture5,String collectionRules,
                                     float cardProbability1,float  cardProbability2,float cardProbability3,float cardProbability4, float cardProbability5,
                                     Date createTime,int status,int participants,int fullCollectors) {
        Long id = Long.valueOf(string_id);
        CardActivity cardActivity = cardActivityDao.findCardActivityById(id);
        if (cardActivity != null) {
            cardActivityDao.updateTitle(id, title);
            cardActivityDao.updatePublicityPicture(id, publicityPicture);
            cardActivityDao.updateStartTime(id, startTime);
            cardActivityDao.updateEndTime(id, endTime);
            cardActivityDao.updateCardType(id, cardType);
            cardActivityDao.updateCardPicture1(id, cardPicture1);
            cardActivityDao.updateCardPicture2(id, cardPicture2);
            cardActivityDao.updateCardPicture3(id, cardPicture3);
            cardActivityDao.updateCardPicture4(id, cardPicture4);
            cardActivityDao.updateCardPicture5(id, cardPicture5);
            cardActivityDao.updateCollectionRules(id, collectionRules);
            cardActivityDao.updateCardProbability1(id, cardProbability1);
            cardActivityDao.updateCardProbability2(id, cardProbability2);
            cardActivityDao.updateCardProbability3(id, cardProbability3);
            cardActivityDao.updateCardProbability4(id, cardProbability4);
            cardActivityDao.updateCardProbability5(id, cardProbability5);
            cardActivityDao.updateCreateTime(id, new Date());
            cardActivityDao.updateStatus(id, status);
            cardActivityDao.updateParticipants(id, participants);
            cardActivityDao.updateFullCollectors(id, fullCollectors);
        }
        return "redirect:/admin/parser";
    }

    public List<CardActivity> findCardActivityByTitle(String title)
    {
        return cardActivityDao.findCardActivityByTitle(title);
    }

    public CardActivity findCardActivityByExactTitle(String title)
    {
        return cardActivityDao.findCardActivityByExactTitle(title);
    }
    public CardActivity findCardActivityById(String id) {
        return cardActivityDao.findCardActivityById(Long.valueOf(id));
    }

    // 对于user而言， 如果下架或者过期就不展示
    public List<CardActivity> findCardActivity(String usertype) {
        List<CardActivity> rawCardActivities = cardActivityDao.findCardActivityByCreateTimeDesc();
        if (usertype.equals("user"))
            rawCardActivities.removeIf(cardActivity -> cardActivity.getStatus() == 0);
        return rawCardActivities;
    }

}

